//
//  GoodsDetailsViewController.m
//  AppleCilent
//
//  Created by Nail Sharipov on 06/04/14.
//  Copyright (c) 2014 Nail Sharipov. All rights reserved.
//

#import "GoodsDetailsViewController.h"
#import "MainProperties.h"
#import "CartManager.h"
#import "SceneManager.h"
#import <QuartzCore/QuartzCore.h>

@interface GoodsDetailsViewController ()

@end

@implementation GoodsDetailsViewController
{
    int goodsQuantity;
    GoodsDomain* goods;
}

- (void) setGoods:(GoodsDomain*) goodsDomain
{
    goods = goodsDomain;
    
    CartOrder* order = [[CartManager getInstance] getOrder:goods];
    goodsQuantity = [order getQuantity];
    
    [self.goodsName setText:[goods getName]];

    int price = (int)[goods getPrice];
    [self.price setText:[NSString stringWithFormat:@"%i р.", price]];

    [self.quantity setText:[NSString stringWithFormat:@"%i", goodsQuantity]];
    [self.descriptionText setText:[goods getDescription]];
    
    NSString* imageName = [goods getImage];
    NSString* imageFolderPath = [[MainProperties getInstance] getImageFolder];
    NSString* imagePath = [NSString stringWithFormat:@"%@/%@", imageFolderPath, imageName];
    UIImage* image = [UIImage imageWithContentsOfFile:imagePath];
    [self.imageView setImage:image];
    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CALayer* layer = [self.goodsDetailsView  layer];
    [layer setCornerRadius:10.0f];
    [layer setMasksToBounds:YES];
    [layer setBorderColor:[UIColor yellowColor].CGColor];
    [layer setBorderWidth:1.0f];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    CartOrder* order = [[CartManager getInstance] getOrder:goods];
    goodsQuantity = [order getQuantity];
    
    int total = (int)[[CartManager getInstance] getTotalCost];
    [[self cartButton] setTitle:[NSString stringWithFormat:@"%i р.", total]forState:UIControlStateNormal];
    
    [self.quantity setText:[NSString stringWithFormat:@"%i", goodsQuantity]];
    [self.descriptionText setText:[goods getDescription]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)backButtonClick:(id)sender {
   [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)cartButtonClick:(id)sender {
    [[SceneManager getInstance] openScene: @"Cart"];
}

- (IBAction)decButtonClick:(id)sender {
    goodsQuantity--;
    if (goodsQuantity < 0) goodsQuantity  = 0;
    
    CartOrder* order = [[CartManager getInstance] getOrder:goods];
    [order setQuantity:goodsQuantity];
    
    [self.quantity setText:[NSString stringWithFormat:@"%i", goodsQuantity]];
    
    int total = (int)[[CartManager getInstance] getTotalCost];
    [[self cartButton] setTitle:[NSString stringWithFormat:@"%i р.", total]forState:UIControlStateNormal];
}

- (IBAction)addButtonClick:(id)sender {
    goodsQuantity++;
    if (goodsQuantity > 50) goodsQuantity  = 50;
    
    CartOrder* order = [[CartManager getInstance] getOrder:goods];
    [order setQuantity:goodsQuantity];
    
    [self.quantity setText:[NSString stringWithFormat:@"%i", goodsQuantity]];
    
    int total = (int)[[CartManager getInstance] getTotalCost];
    [[self cartButton] setTitle:[NSString stringWithFormat:@"%i р.", total]forState:UIControlStateNormal];
}

@end
